#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>


int main(int argc, char* argv[])
{
    char* file = "test.txt";
    if (argc == 2) {
        file = argv[1];
    }

    int fd = open(file, O_WRONLY | O_CREAT, 0664);
    if (fd < 0) {
        perror("open failed");
        return 1;
    }

    // echo 写文件
    char buf[64];
    while (1) {
        printf("in\n");
        fgets(buf, sizeof(buf) - 1, STDIN_FILENO);
        if(strncmp("exit",buf,4) == 0 || strncmp("quit",buf,4) == 0 )
            break;

        write(fd, buf, strlen(buf));
    }
    close(fd);

    return 0;
}
